// 配置验证工具
import config from './constants'
import { ConfigValidationResult } from '../../src/types'

// 验证配置函数
export const validateConfig = (): ConfigValidationResult => {
  const result: ConfigValidationResult = {
    isValid: true,
    errors: [],
    warnings: [],
  }

  // 验证API基础URL
  if (!config.API_BASE_URL) {
    result.errors.push('API_BASE_URL is not configured')
    result.isValid = false
  } else if (!config.API_BASE_URL.startsWith('http')) {
    result.warnings.push('API_BASE_URL should start with http:// or https://')
  }

  // 验证应用名称
  if (!config.APP_NAME) {
    result.warnings.push('APP_NAME is not configured')
  }

  // 验证版权年份
  const copyrightYear = Number(config.COPYRIGHT_YEAR)
  const currentYear = new Date().getFullYear()
  if (isNaN(copyrightYear)) {
    result.warnings.push('COPYRIGHT_YEAR is not a valid number')
  } else if (copyrightYear < 2000 || copyrightYear > currentYear + 1) {
    result.warnings.push(`COPYRIGHT_YEAR should be between 2000 and ${currentYear + 1}`)
  }

  // 验证环境
  const validEnvs = ['development', 'production', 'test']
  if (!validEnvs.includes(config.NODE_ENV)) {
    result.warnings.push(`NODE_ENV should be one of: ${validEnvs.join(', ')}`)
  }

  return result
}

// 打印配置信息
export const printConfig = (): void => {
  console.log('=== Application Configuration ===')
  console.log('App Name:', config.APP_NAME)
  console.log('App Version:', config.APP_VERSION)
  console.log('Company Name:', config.COMPANY_NAME)
  console.log('Copyright Year:', config.COPYRIGHT_YEAR)
  console.log('API Base URL:', config.API_BASE_URL)
  console.log('Environment:', config.NODE_ENV)
  console.log('Is Development:', config.NODE_ENV === 'development')
  console.log('Is Production:', config.NODE_ENV === 'production')
  console.log('===============================')
}

// 检查配置并打印结果
export const checkConfig = (): void => {
  const validation = validateConfig()
  
  if (!validation.isValid) {
    console.error('Configuration validation failed:')
    validation.errors.forEach(error => console.error(`- ${error}`))
  }
  
  if (validation.warnings.length > 0) {
    console.warn('Configuration warnings:')
    validation.warnings.forEach(warning => console.warn(`- ${warning}`))
  }
  
  if (validation.isValid && validation.warnings.length === 0) {
    console.log('Configuration is valid')
  }
  
  printConfig()
}

// 默认导出
export default {
  validateConfig,
  printConfig,
  checkConfig,
}